<template>
	<view class="container">

		<view class="nav">
			<view class="nav__item" :class="tabIdx == item.id ? '--focus' : ''" v-for="(item,index) in navList"
				:key="index" @click="changeNavItem(item.id)">
				{{item.name}}
			</view>
			<view class="nav__bottom" :style="{'transform':activeBottom}"></view>
		</view>

		<mescroll-body ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback">
			<view class="content">
				<view class="list" v-for="(item,index) in orderList" :key="index" @click.stop="toOrderDetail(item.id)">
					<view class="list__title">
						<view class="sn">订单号：{{item.order_sn}}</view>
						<view class="status">{{item.status_name}}</view>
					</view>
					<view class="list__info">
						<view class="shop" v-for="(goodsItem, goodsIdx) in item.has_many_order_goods" :key="goodsIdx">
							<view class="shop__msg">
								<image :src="goodsItem.thumb"></image>
								<view class="title">{{goodsItem.title}}</view>
							</view>
							<view class="shop__price">
								<text class="price">￥{{goodsItem.goods_price}}</text>
								<text class="total">×{{goodsItem.total}}</text>
							</view>
						</view>
						<view class="total-price">
							共{{item.goods_total}}件商品 实付：￥<view class="bold">{{item.price}}</view>
						</view>
					</view>
					<view class="list__btn" v-if="item.status != 1">
						<view class="item" :class="(i.value == 1 || i.value == 5) ? 'item--green' : ''"
							v-for="(i, idx) in item.button_models" :key="idx" @click.stop="handleOrderBtn(item, i)">
							{{i.name}}
						</view>
					</view>
				</view>
			</view>
		</mescroll-body>
	</view>
</template>

<script>
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	export default {
		mixins: [MescrollMixin],
		data() {
			return {
				tabIdx: 1,
				page: 1,
				apiUrl: '',
				apiParams: {},
				navList: [{
						id: 0,
						name: '全部'
					},
					{
						id: 1,
						name: '待付款'
					},
					{
						id: 2,
						name: '待发货'
					},
					{
						id: 3,
						name: '待收货'
					},
					{
						id: 4,
						name: '已完成'
					},
				],
				orderList: []
			};
		},
		onLoad(options) {
			if (options.tabIdx) {
				this.tabIdx = options.tabIdx
				this.init()
			}
		},
		computed: {
			// 激活时的滑块位置
			activeBottom() {
				if (this.tabIdx == 1) {
					return 'translateX(190rpx)';
				} else if (this.tabIdx == 2) {
					return 'translateX(330rpx)';
				} else if (this.tabIdx == 3) {
					return 'translateX(470rpx)';
				} else if (this.tabIdx == 4) {
					return 'translateX(600rpx)';
				} else {
					return 'translateX(50rpx)';
				}
			}
		},
		methods: {
			// 初始化
			init() {
				this.initNavStatus();
				this.getOrderList();
			},
			// 获取订单列表
			getOrderList() {
				this.$api.get(this.apiUrl, this.apiParams).then(res => {
					this.orderList = res.data.data
				})
			},
			// 初始化导航状态
			initNavStatus() {
				if (this.tabIdx == 0) {
					this.apiUrl = '/addons/yun_shop/api.php?route=order.list'
					this.apiParams = {
						page: this.page
					}
				} else if (this.tabIdx == 1) {
					this.apiUrl = '/addons/yun_shop/api.php?route=order.list.waitPay'
					this.apiParams = {
						page: this.page
					}

				} else if (this.tabIdx == 2) {
					this.apiUrl = '/addons/yun_shop/api.php?route=order.list.waitSend'
					this.apiParams = {
						page: this.page
					}

				} else if (this.tabIdx == 3) {
					this.apiUrl = '/addons/yun_shop/api.php?route=order.list.waitReceive'
					this.apiParams = {
						page: this.page
					}

				} else {
					this.apiUrl = '/addons/yun_shop/api.php?route=order.list.Completed'
					this.apiParams = {
						page: this.page,
						mid: '',
						uuid: '',
						shop_id: ''
					}
				}
				console.log(this.apiUrl, this.apiParams);
			},
			// 点击导航栏切换
			changeNavItem(id) {
				this.tabIdx = id;
				this.page = 1;
				this.orderList = [];
				this.initNavStatus();
				this.downCallback();
			},

			// 点击订单相关按钮
			handleOrderBtn(item, btnItem) {
				console.log(item, btnItem);
				if (btnItem.value == 8) {
					this.$Router.push({path: '/pages/prefile/express-list/express-list', query: {id: item.id}})
				} else if (btnItem.value == 41) {
					this.$Router.push({path: '/pages/prefile/service/service'})
				} else if (btnItem.value == 1) {
					this.$Router.replace({
						path:  `/pages/order/payment/method`,
						query: {
							orderId: item.id,
							goodsId: item.has_many_order_goods.goods_id,
							payState: 'paying',
						}
					});
				} else {
					let url = `/addons/yun_shop/api.php?route=${btnItem.api}`
					let params = {
						order_id: item.id
					}
					uni.showModal({
						title: '提示',
						content: `确认要${btnItem.name}吗？`,
						success: res => {
							if (res.confirm) {
								this.$api.get(url, params).then(res => {
									this.$u.toast(`${btnItem.name}成功！`);
									this.init();
								})
							}
						}
					})
				}
			},
			// 去订单详情
			toOrderDetail(id) {
				uni.navigateTo({
					url: `/pages/prefile/my-order/detail/order-detail?order_id=${id}`
				})
			},

			// 下拉刷新列表
			downCallback() {
				this.mescroll.resetUpScroll(); // 重置列表为第一页 (自动执行 page.num=1, 再触发upCallback方法 )
			},
			/*上拉加载的回调*/
			async upCallback(page) {
				this.page = page.num;
				// this.mescroll.endBySize(1, 0);
				this.$api.get(this.apiUrl, this.apiParams).then(res => {
					let len = res.data.data.length
					let total = res.data.total
					if (page.num == 1) {
						this.orderList = []; //如果是第一页需手动置空列表
					}
					this.orderList = this.orderList.concat(res.data.data); //追加新数据
					this.mescroll.endBySize(len, total);
				})
			},
		}
	}
</script>

<style lang="scss">
	@import './list.scss'
</style>
